package com.w.wx.service.impls;


import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.w.wx.domain.*;
import com.w.wx.mapper.*;
import com.w.wx.service.UserService;
import com.w.wx.utils.ALToHMUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;


import java.util.*;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserErMapper userErMapper;
    @Autowired
    private UserDeMapper userDeMapper;
    @Autowired
    private ShoppingCarMapper shoppingCarMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private ImagesMapper imagesMapper;
    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    public int setUserInfo(UserEr userinfo) {
        //获取微信用户昵称头像等信息

       String openid = userinfo.getOpenid();
        UserEr userEr = userErMapper.selectByOpenid(openid);
        if(userEr==null){
            userinfo.setOpenid(openid);
            userinfo.setState(0);
            return userErMapper.insert(userinfo);
        }

        return 0;
    }

    @Override
    public UserEr getUserInfo(String openid) {
        return userErMapper.selectByOpenid(openid);
    }

    @Override
    public int updateUserInfo(UserDe userDetailInfo) {

        String openid =userDetailInfo.getOpenid();
       UserDe userDe =  userDeMapper.selectByOpenid(openid);
       int a=0,b=0;
       if(userDe == null){
           //第一次修改为新增用户信息
           userDetailInfo.setOpenid(openid);
           a = userDeMapper.insert(userDetailInfo);
       }else{
           //之后每次修改都为更新用户信息
           userDetailInfo.setOpenid(openid);
           userDetailInfo.setUuid(userDe.getUuid());
           b =  userDeMapper.updateByOpenid(userDetailInfo);
       }

       if(a==1 || b==1){
           return 1;
       }else{
           return 0;
       }

    }

    @Override
    public UserDe getUserDeInfo(String openid) {
       return userDeMapper.selectByOpenid(openid);
    }

    @Override
    public int addGoodInCar(String openid, Integer gid) {
        ShoppingCar car = new ShoppingCar();
        car.setOpenid(openid);
        car.setGid(gid);
        return shoppingCarMapper.insert(car);

    }

    @Override
    public ArrayList<Goods> getMyCarSimpleInfo(String openid) {
        ArrayList<Integer> gids = shoppingCarMapper.selectByOpenid(openid);
        ArrayList<Goods> goods = new ArrayList<>();
        Iterator<Integer> it = gids.iterator();
        while(it.hasNext()){
            Goods good = goodsMapper.selectMyCarSimByGid(it.next());
            goods.add(good);
        }

        return goods;
    }

    @Override
    public UserEr getErUserAndImage(String openid) {
        UserEr userEr = userErMapper.selectByOpenid(openid);
        ArrayList<Images> image = imagesMapper.selectByOpenid(openid);
        userEr.setImage(image);
        return userEr;
    }

    @Override
    public ArrayList<UserEr> getAllUserSimInfo() {
        return userErMapper.selectAll();
    }

    @Override
    public ArrayList<Integer> getFavoritesByOpenid(String openid) {
        return shoppingCarMapper.selectByOpenid(openid);
    }

    @Override
    public int addGoodsInCar(@RequestBody JSONObject sums) {
        String openid = sums.getString("openid");
        JSONArray gid = sums.getJSONArray("gid");
        if (openid==null||gid==null){
            return 0;
        }
        JSONArray  gidf = (JSONArray)gid.clone();
        ArrayList<Integer> integers = shoppingCarMapper.selectByOpenid(openid);
        int a = 1;
        if (integers.size()==0 ){
            ShoppingCar car = new ShoppingCar();
            car.setOpenid(openid);
            car.setGid(gid.getInteger(0));
            a = shoppingCarMapper.insert(car);
        }
        if (!integers.isEmpty()){
            //去重

                Iterator<Object> it = gid.iterator();
                while(it.hasNext()){
                    Integer gid1 =(Integer) it.next();

                    integers.forEach(item -> {
                        if(item == gid1){
                            it.remove();
                        }
                    });
                }

                Iterator<Object> it1 = gid.iterator();

                while (it1.hasNext() && a==1){
                    int next = (int)it1.next();
                    ShoppingCar car = new ShoppingCar();
                    car.setOpenid(openid);
                    car.setGid(next);
                    a = shoppingCarMapper.insert(car);
                }

                Iterator<Integer> integers0 = integers.iterator();

                while(integers0.hasNext()){
                    Integer gid1 =integers0.next();
                    gidf.forEach(item -> {
                        if(item == gid1){
                            integers0.remove();
                        }
                    });
                }
                Iterator<Integer> iterator2 = integers.iterator();

                while (iterator2.hasNext() && a==1){
                    int next = iterator2.next();
                    ShoppingCar car = new ShoppingCar();
                    car.setOpenid(openid);
                    car.setGid(next);
                    a = shoppingCarMapper.deleteCar(car);
                }


            if (a==0){
                return 0;
            }else {
                return 1;
            }
        }
       return 0;

    }

    @Override
    public Msg setUserCheckState(String openid, Integer state, String reason) {

        UserEr userEr = userErMapper.selectByOpenid(openid);
        if (userEr.getState()==1 || userEr.getState()==2){
            return Msg.fail().add("error","你已审核");
        }
        if (userEr.getState() == 10){
            return Msg.fail().add("error","你已拉黑");
        }
        if(state==2){
            //审核不通过
            Notice notice = new Notice();
            notice.setFromOpenid("system");
            notice.setNoticeInfo(reason);
            notice.setToOpenid(openid);
            notice.setGid(0);
            notice.setSendTime(new DateTime());
            log.info("200+ "+notice.toString());
            noticeMapper.insert(notice);
            //审核失败随之删除用户认证图片，等待再次上传
            imagesMapper.deleteByOpenid(openid);
            return ALToHMUtil.retrunEasy(userErMapper.updateByOpenid(openid,2));
        }else if (state==1){
            //审核通过
            return ALToHMUtil.retrunEasy(userErMapper.updateByOpenid(openid,1));
        }else if (state == 10){
            //拉黑
            Notice notice = new Notice();
            notice.setFromOpenid("system");
            notice.setNoticeInfo(reason);
            notice.setToOpenid(openid);
            notice.setGid(0);
            notice.setSendTime(new DateTime());
            noticeMapper.insert(notice);
            //审核失败随之删除用户认证图片，等待再次上传
            imagesMapper.deleteByOpenid(openid);
            return ALToHMUtil.retrunEasy(userErMapper.updateByOpenid(openid,10));
        }
        return Msg.fail();
    }

    @Override
    public Msg userCheckIsOk(String openid) {
        Notice notice = noticeMapper.selectByTwoOpenid(openid);
        UserEr userEr = userErMapper.selectByOpenid(openid);
        Map<String,Object> map = new HashMap<>();

        if (notice==null || userEr.getState()==0){
            map.put("state",0);
            map.put("result","未认证");
        }else if (userEr.getState()==1){
            map.put("state",1);
            map.put("result","认证成功");
        }else if(userEr.getState()==2){
            //审核不通过
            map.put("state",2);
            map.put("result",notice);
        }else if(userEr.getState()==3){
            map.put("state",3);
            map.put("result","待审核");
        }else {
            //拉黑
            map.put("state",10);
            map.put("result",notice);
        }
        Msg msg = new Msg();
        msg.setMsg("处理成功");
        msg.setCode(200);
        msg.setExtend(map);
        return msg;
    }

    @Override
    public Msg isRegister(String openid) {

        UserEr userEr = userErMapper.selectByOpenid(openid);

        if (userEr==null){
            return Msg.success().add("tip","未注册");
        }else{
            return Msg.fail().add("tip","已注册");
        }
    }
}
